package com.code.controller;

import com.code.model.HeroModel;
import com.code.model.ProductModel;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.ArrayList;
import java.util.List;

@Controller
public class HomeController {



    private final String[] names = {"司马懿","吕布","甘宁","陆逊","孙策",
            "赵云","张辽","关羽","荀彧","曹操","孙权","刘备","周瑜","诸葛亮","张飞","姜维"};

    @GetMapping("index")
    public String index(Model model) {
        model.addAttribute("message", "Welcome to Thymeleaf !!!");
        return "index";
    }
    @GetMapping("product")
    public String hero(Model model) {
        model.addAttribute("message", "Welcome to Thymeleaf !!!");
        HeroModel hero = new HeroModel();
        hero.setId(1);
        hero.setName("关羽");

        hero.setImagePath("/img/img8.png");
        model.addAttribute("hero ", hero);
        return "hero";
    }
    @GetMapping("hero/{id}")
    public String hero(Model model , @PathVariable int id) {
        model.addAttribute("message", "欢迎来到三国！");
        HeroModel hero = new HeroModel();
        hero.setId(id);
        hero.setName(names[id]);

        hero.setImagePath("/img/img" + (id + 1) + ".png");


        model.addAttribute("hero", hero);
        return "hero";
    }

    @GetMapping("heroes")
    public String products(Model model) {

        List<HeroModel> heroes = new ArrayList<>();
        for (int i = 0 ; i < 16; i++) {
            HeroModel hero = new HeroModel();
            hero.setId(i);
            hero.setName(names[i]);

            hero.setImagePath("/img/img" + (i + 1) + ".png");
            heroes.add(hero);
        }

        model.addAttribute("heroes", heroes);
        return "heroes";
    }



}
